
#include "stringutils.h"
#include <string.h>

int str_equal(char str1[], char str2[])
{
    if (NULL == str1 || NULL == str2)
    {
        return -1;
    }
    
    if(strlen(str1) != strlen(str2))
    {
        return -1;
    }
    
    int len = strlen(str1);
    int i=0;
    for(i=0; i<len; i++)
    {
        if(*(str1+i) != *(str2+i))
        {
            return -1;
        }
    }
    
    return 0;
    
}

int trim(char *s)
{
    int i, j = -1, len = 0;
 
    for (i = 0; s[i] != '\0'; i++)
    {
        if (j == -1 && s[i] != ' ' && s[i] != '\t')
        {
            j = 0;
        }
        if (j != -1)
        {
            s[j] = s[i];
            j++;
            if (s[i] != ' ' && s[i] != '\t')
            {
                len = j;
            }
        }
    }
    s[len] = '\0';
    return len;
}

char *str_replace(const char *search, const char *replace, char *subject, int bufsize)
{
    char *p;
    int i, j = 0, n = 0;
    int len, slen, rlen, tlen;
    
    // check arguments
    if (subject == NULL)
    {
        return NULL;
    }
    tlen = strlen(subject);
    if (bufsize == -1)
    {
        // make sure subject is writable
        p = malloc(tlen + 1);
        if (p == NULL)
        {
            return NULL;
        }
        memcpy(p, subject, tlen + 1);
        subject = p;
    }
    if (search == NULL || search[0] == '\0')
    {
        return subject;
    }
    slen = strlen(search);
    if (replace == NULL)
    {
        replace = "";
    }
    rlen = strlen(replace);
 
    // find out how many replacements are needed
    for (i = 0; subject[i] != '\0'; i++)
    {
        if (subject[i] != search[j])
        {
            // character mismatch
            j = 0;
        }
        if (subject[i] == search[j])
        {
            j++;
            if (j == slen)
            {
                // pattern match
                subject[i + 1 - j] = '\0'; // front
                subject[i] = '\0'; // rear
                j = 0;
                n++;
            }
        }
    }
    if (n == 0)
    {
        return subject;
    }
 
    // calculate the length of the new string and check buffer size
    len = tlen + n * (rlen - slen);
    if (bufsize == -1)
    {
        if (len > tlen)
        {
            // increase buffer size
            p = realloc(subject, len + 1);
            if (p == NULL)
            {
                free(subject);
                return NULL;
            }
            subject = p;
        }
    }
    else if (bufsize < len + 1)
    {
        return NULL;
    }
 
    // replace substrings
    if (len <= tlen)
    {
        i = 0; // for read
        j = 0; // for write
        while (i < tlen)
        {
            if (subject[i] == '\0')
            {
                memcpy(subject + j, replace, rlen);
                i += slen;
                j += rlen;
            }
            else
            {
                subject[j] = subject[i];
                i++;
                j++;
            }
        }
    }
    else
    {
        i = tlen - 1;
        j = len - 1;
        while (i >= 0)
        {
            if (subject[i] == '\0')
            {
                memcpy(subject + j + 1 - rlen, replace, rlen);
                i -= slen;
                j -= rlen;
            }
            else
            {
                subject[j] = subject[i];
                i--;
                j--;
            }
        }
    }
    subject[len] = '\0';
    return subject;
}
 
